package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.table.AbstractTableModel;

public class EmpTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 3099768691830690602L;
	private ArrayList<Object> data;
	private ArrayList<String> columnNames;

	public EmpTableModel() {
		this.data = new ArrayList<Object>();
		this.columnNames = new ArrayList<String>(Arrays.asList("STT", "Họ nhân viên",
				"T\u00EAn nhân viên", "Ch\u1EE9c v\u1EE5", "Ng\u00E0y  l\u00E0m",
				"\u0110i\u1EC7n tho\u1EA1i"));
		loadData();
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return (columnIndex != 0) ? super.getColumnClass(columnIndex)
				: Integer.class;
	}

	@Override
	public Object getValueAt(int row, int column) {
		Employee e = (Employee) data.get(row);

		switch (column) {
		case 0:
			return row + 1;
		case 1:
			return e.getFirstName();
		case 2:
			return e.getLastName();
		case 3:
			return e.getRegency();
		case 4:
			return e.getDateBegin();
		case 5:
			return e.getPhone();
		}
		return null;
	}

	public void loadData() {
		Connection connect = null;
		try {
			connect = Database.connect();
			data.clear();
			PreparedStatement pre = connect
					.prepareStatement("SELECT * FROM employee ORDER BY last_name ASC");
			ResultSet rs = pre.executeQuery();

			while (rs.next()) {
				Employee e = new Employee();
				e.setId(rs.getInt("id_empl"));
				e.setFirstName(rs.getString("first_name"));
				e.setLastName(rs.getString("last_name"));
				e.setBirthday(rs.getDate("birthday"));
				e.setSex(rs.getString("sex"));
				e.setIdent(rs.getString("identification"));
				e.setAddress(rs.getString("address"));
				e.setPhone(rs.getString("phone"));
				e.setDateBegin(rs.getDate("date_begin"));
				e.setRegency(rs.getString("regency"));

				this.data.add(e);
			}

			fireTableDataChanged();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (connect != null) {
					connect.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public int getMaxIDEmpl() {
		Connection connect = null;
		try {
			connect = Database.connect();
			PreparedStatement pre = connect
					.prepareStatement("SELECT max(id_empl) FROM employee");
			ResultSet rs = pre.executeQuery();

			if (rs.first())
				return rs.getInt(1);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (connect != null)
				try {
					connect.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		return 0;
	}

	@Override
	public int getRowCount() {
		if (data == null)
			return 0;
		return data.size();
	}

	@Override
	public int getColumnCount() {
		return columnNames.size();
	}

	@Override
	public String getColumnName(int column) {
		return columnNames.get(column);
	}

	// use to get employee from data table
	public Object getRow(int selectRow) {
		return this.data.get(selectRow);
	}

}
